﻿@model App.Web.ViewModels.UtilityModels.ListConfig
@using App.Web.ViewModels.UtilityModels;

@{
    ListConfig _Config = Model as ListConfig;    
    System.Data.DataTable dt = _Config.DataSource;
}
@if (dt != null && dt.Rows.Count > 0)
{
    int counter = 0;
    <table class="list-view" cellpadding="3" cellspacing="0">
        <tr>
            @foreach (Group group in _Config.Groups)
            {
                <th>@group.Name</th>
            }
            @if (_Config.ShowEditLink)
            {
                <th style="text-align:center;">Action</th>
            }
            @if (_Config.ShowExternalLink)
            {
                <th style="text-align:center;">Manage User</th>
            }
        </tr>
        @foreach (System.Data.DataRow row in dt.Rows)
        {
            String className = "odd-row";
            if(counter % 2 == 0){
                className = "even-row";
            }
            counter++;
            <tr class="@className">
                @foreach (Group group in _Config.Groups)
                {
                    <td>
                        @foreach (Field field in group.Fields)
                        {
                            if (String.IsNullOrEmpty(field.StringFormat))
                            {
                            <div>
                                <b>@field.Name: </b> @Html.FormatData(row[field.Name])
                            </div>
                            }
                            else
                            {
                                List<String> values = new List<String>();
                                foreach (String fieldName in field.Params)
                                {
                                    ///Need to Convert and Format the Value Strings according to their Type
                                    values.Add(Html.FormatData(row[fieldName]));
                                }
                                <div>
                                    @Html.Raw(String.Format(field.StringFormat, values.ToArray()))
                                </div>
                            }
                        }
                    </td>
                }
                @if (_Config.ShowEditLink)
                {
                    <td style="text-align:center;"><a href="@String.Format(_Config.EditLinkFormat, row["ID"])">Edit</a></td>
                }
                @if (_Config.ShowExternalLink)
                {
                    if (row["UserName"].ToString().Trim().IsEmpty())
                    {
                        <td style="text-align:center;"><a href="@String.Format(_Config.ExternalLinkFormat, row["ID"], row["UserName"])">Create User</a></td>
                    }
                    else
                    {
                        <td style="text-align:center;"><a href="@String.Format(_Config.ExternalLinkFormat, row["ID"], row["UserName"])">Edit User</a></td>
                    }
                }
            </tr>
        }
    </table>
}